require(['app/Compiler', 'app/VNode'], function(Compiler, VNode) {

    var xml =
        `
    <!--index.wxml-->
        <view class="container">
            <view class="userinfo">
                <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
                <block wx:else>
                    <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
                    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
                </block>
            </view>
            
            <view class="usermotto">
                <text class="user-motto">{{motto}}</text>
            </view>
            <view wx:for="{{array}}">{{item}} </view>
            <view wx:if="{{view == 'WEBVIEW'}}"> WEBVIEW </view>
            <view wx:elif="{{view == 'APP'}}"> APP </view>
            <view wx:else="{{view == 'MINA'}}"> MINA </view>
        </view>
                        

    `;


    var data = {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: function() {
            return true;
        },
        array: [1, 2, 3, 4, 5],
        view: "MINA",
        staffA: {firstName: 'Hulk', lastName: 'Hu'}
    };

    //----- DataProxy -----
    function DataProxy(data) {
        this.data = data;

        Object.keys(data).forEach(function (key) {
            this.addAccessor(key);
        }.bind(this));
    }

    DataProxy.prototype.addAccessor = function(key) {
        
        var self = this;
        Object.defineProperty(this, key, {
            "enumerable" : true,
            "configurable" : true,
            get: function() {
                return self.data[key];
            }
        });
    };

    (function testDataProxy() {
        var proxy = new DataProxy(data);

        console.log("----------");
        console.log(proxy["message"]);
        console.log(proxy["array"][0].name);
    }());



    function printNodeTree(node, depth) {

        function stringWithIntent(text, depth) {
            var intent = "";
            for (var i = 0; i < depth; ++i) {
                intent += "  ";
            }
            return intent + text;
        }

        if (node.tag) {
            console.log(stringWithIntent("<" + node.tag + ">" + " " + JSON.stringify(node.unhandleAttrs), depth));
        }
        else {
            console.log(node.text);
        }

        var count = node.getChildCount();
        for (var i = 0; i < count; ++i) {
            printNodeTree(node.getChild(i), depth + 1);
        }
        if (node.tag) {
            console.log(stringWithIntent("</" + node.tag + ">", depth));
        }
    }


    var root = Compiler.createNodeTreeFromXML(xml);
    console.log('----------');
    printNodeTree(root, 0);

    var code = Compiler.genCodeForNode(root);
    console.log('----------');
    console.log(code);


    var vroot = VNode.createVNodeByCode(data, code);
    console.log('----------');

    VNode.mountVNodeToBody(document.body, vroot);

});